package com.delu.controller;

import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.service.IWalletService;
import com.delu.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("wallet")
@ResponseBody
@Api(value = "用户管理系统")
public class WalletControllre {
    @Autowired
    private IWalletService iWalletService;
    @Autowired
    private JwtUtil jwtUtil;

    @ApiOperation(value = "个人钱包查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户名", required = true, paramType = "path"),
            @ApiImplicitParam(name = "type", value = "查询钱包的类型,如:余额为 1 ,押金为 2", required = true, paramType = "path")
    })
    @GetMapping("select")
    public Result SelectWalletByUserId(@RequestParam("type") int type, HttpServletRequest request) {
        Long userId = null;
        try {
            String authToken = request.getHeader("authToken");
            //使用jwt工具类解析token中存放的内容
            Claims claims = jwtUtil.parseJwt(authToken);
            //获取对象id
            String id = claims.getId();
            //获取存入的用户对象
            claims.getSubject();
            //将jwt封装的用户转成Long
            userId = Long.valueOf(id);
            //根据前台传来的用户id查询钱包
            if (type == 1) {
                //type为1,表示查询账户余额
                Result result = iWalletService.SelectWalletByUserId(userId, type);
                return result;
            }
            if (type == 2) {
                //type为1,表示查询用户押金
                Result result = iWalletService.SelectWalletByUserId(userId, type);
                return result;
            }
            if (type == 3) {
                //type 为 3 表示查询用户储存表
                Result result = iWalletService.SelectWalletByUserId(userId, type);
                return result;
            }
            if (type == 4){
                //type 为4 表示查询积分
                Result result = iWalletService.SelectWalletByUserId(userId, type);
                return result;
            }
            } catch (ExpiredJwtException e) {

                return Result.response(ResultEnum.ERROR);
            }
            return Result.response(ResultEnum.SUCCESS);
    }

}
